const http = require('http')
const querystring = require('querystring')

const server = http.createServer((req, res) => {
    console.log(req.url)
    if (req.url === '/dopost' && req.method === 'POST') {
        let alldata = ''

        req.on('data', chunk => {
            alldata += chunk
        })

        req.on('end', () => {
            const datastring = alldata.toString()
            res.end('success' + alldata)
            const dataObj = querystring.parse(datastring)
            console.log(dataObj)
            console.log(dataObj.name)
            console.log(dataObj.sex)
        })
    } else {
        res.end(req.method + '-' + req.url)
    }
})

server.listen(80, '127.0.0.1', () => {
    console.log('server running on port 80')
})
